import { createRouter, createWebHistory } from 'vue-router'
import loginView from '@/views/login/index.vue'
import zcView from '@/views/login/zc.vue'
import homeView from '@/views/home/home.vue'
import indexView from '@/views/home/index.vue'
import fenlView from '@/views/home/fenl.vue'
import gwcView from '@/views/home/gwc.vue'
import myView from '@/views/home/my.vue'
import ssView from '@/views/home/ss.vue'
import sslbView from '@/views/home/sslb.vue'
import xqView from '@/views/home/xq.vue'


const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/', redirect: '/home/index' },
    { path: '/login', name: 'login', component: loginView, },
    { path: '/zc', name: 'zc', component: zcView, },
    {
      path: '/home', name: 'home', component: homeView, children: [
        { path: 'index', name: 'index', component: indexView, },
        { path: 'fenl', name: 'fenl', component: fenlView, },
        { path: 'gwc', name: 'gwc', component: gwcView, },
        { path: 'my', name: 'my', component: myView, },
        { path: 'ss', name: 'ss', component: ssView, },
        { path: 'sslb:/cat_name', name: 'sslb', component: sslbView, },
        { path: 'xq:/goods_id', name: 'xq', component: xqView, },
      ]
    },
  ],
})
router.beforeEach((to, from, next) => {
  if (to.fullPath == '/home/index' || to.fullPath == '/home/fenl') {
    const token = localStorage.getItem('token')
    if (!token) {
      next('/login')
    } else {
      next()
    }
  } else {
    next()
  }
})
export default router
